# -*- encoding: UTF-8 -*-

'''
Created on 06/01/2013

@author: jotagê
'''
from datetime import datetime
from sqlalchemy import Column, ForeignKey , create_engine
from sqlalchemy.dialects.mysql import BIGINT, DATETIME, TEXT , TINYINT , VARCHAR
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.interfaces import MapperExtension

Base = declarative_base()

class BaseExtension(MapperExtension):
    
    def before_insert(self, mapper ,connection, instance ):
        instance.created_at = datetime.now()
        
    def before_update(self, mapper ,connection, instance ):
        instance.update_at = datetime.now()



class Country(Base):
    
    __tablename__ = 'country'
    __table_args = {
                    'mysql_engine':'InnoDB',
                    'mysql_charset':'utf8'
                    }
    __mapper_args = {'extension':BaseExtension()}
    
    id = Column('country_id',BIGINT(unsigned=True), primary_key=True)
    code = Column(VARCHAR(10), nullable= False , unique= True)
    name = Column(VARCHAR(100), nullable= False, unique= True)
    created_by_user= Column(VARCHAR(50))
    created_at = Column(DATETIME)
    updated_by_user = Column(VARCHAR(50))
    update_at = Column(DATETIME)
    
    def __init__(self, code, name):
        self.code = code
        self.name = name
        
class Category(Base):
    __tablename__ = 'category'
    __table_args = {
                    'mysql_engine':'InnoDB',
                    'mysql_charset':'utf8'
                    }
    
    id = Column('category_id', BIGINT(unsigned= True), primary_key= True)
    name = Column(VARCHAR(100), nullable= False , unique = True)
    created_at = Column(DATETIME)  
    updated_by_user = Column(VARCHAR(50))  
    updated_at = Column(DATETIME) 
    
    def __init__(self, name):
        self.name = name
           
class Customer(Base):
    __tablename__ = 'customer'
    __table_args = {
                    'mysql_engine':'InnoDB',
                    'mysql_charset':'utf8'
                    }
    id = Column('customer_id', BIGINT(unsigned = True), primary_key = True)
    company_name = Column(VARCHAR(100), nullable=False , unique = True)
    category_id = Column(BIGINT(unsigned = True), ForeignKey('category.category_id'), 
                         name = 'fk_customer_category', onupdate='CASCADE', ondelete='RESTRICT')
    
    contact_title = Column(VARCHAR(50))  
    contact_first_name = Column(VARCHAR(50))  
    contact_last_name = Column(VARCHAR(50))  
    address = Column(TEXT)  
    city = Column(VARCHAR(50))  
    region = Column(VARCHAR(50))  
    postal_code = Column(VARCHAR(50))
    country_id = Column(BIGINT(unsigned= True), ForeignKey ='country.country_id',
                        name='fk_customer_country', onupdate='CASCADE', ondelete='RESTRICT') 
    
    phone = Column(VARCHAR(50))  
    fax = Column(VARCHAR(50))  
    mobile = Column(VARCHAR(50))  
    email = Column(VARCHAR(50))  
    homepage = Column(VARCHAR(50))  
    skype = Column(VARCHAR(50)) 
    active = Column(TINYINT(1), nullable=False, default=1)  
    notes = Column(TEXT)  
    created_by_user = Column(VARCHAR(50))  
    created_at = Column(DATETIME)  
    updated_by_user = Column(VARCHAR(50))  
    updated_at = Column(DATETIME)
    
    def __init__(self, company_name, category_id):
        self.company_name = company_name
        self.category_id = category_id               

